#!/bin/sh
####################################################################
#
# WLAN Initialization script
#
# This script will initialize the wlan modules required for operation
# of the AP hardware.  There are several environmental variables that
# can be set to modify the operation of the AP.  These are set to
# default values (or not used) if not defined.  Parameters are:
#
#  DFS_domainoverride   0=Unitialized (default),
#                       1=FCC Domain (FCC3, US)
#                       2=ETSI Domain (Europe)
#                       3=Japan Domain
#  DFS_usenol           1=Use channel NOL (default)
#  ATH_countrycode      Set Country Code (  Override the use of channel NOL
#                       Override default country code (-1 auto, default)
#                       Use country code 0x1ff for demo channels
#
###################################################################
. /etc/ath/apcfg

###################################################################
## Function to unload all modules
###################################################################

_ath_unload()
{
#    rmmod wlan_scan_ap
#    rmmod wlan_scan_sta
    rmmod ath_pktlog
#    sleep 2
#    rmmod wlan_me
    sleep 2
    rmmod umac
    sleep 2
    rmmod ath_dev
#    rmmod hst_tx99
    rmmod ath_dfs
#    rmmod ath_spectral
    rmmod ath_rate_atheros
    rmmod ath_hal
    rmmod asf
    rmmod adf
}

KVER=`uname -r | cut -f 1 -d '-'`
#MODULE_PATH=/lib/modules/2.6.36+
if [ -f /lib/modules/`uname -r`/ath_hal.ko ]; then
	MODULE_PATH=/lib/modules/`uname -r`
else
	echo "Could not find WiFi modules at /lib/modules !"
	exit 1
fi

###################################################################
## The input parameter is either up or down.  We assume that
## if no argument is provided, UP is intended
###################################################################

echo "Args: $#"

if [ $1 != down ]; then
    #
    # Determine if the modules are already up.  If so, simply
    # exit without error.
    #

    MODLIST=`lsmod | grep ath_hal`

    if [ "${MODLIST}" != "" ]; then
	rm -f /tmp/.apup
        exit 0
	fi

	#
    # Calculate the parameter values required.  Add the appropriate settings
    # to the strings
    #
    
    DFS_ARGS=""
    if [ "${DFS_domainoverride}" != "" ]; then
        DFS_ARGS="domainoverride=$DFS_domainoverride $DFS_ARGS"
    fi
    if [ "${DFS_usenol}" != "" ]; then
        DFS_ARGS="usenol=$DFS_usenol $DFS_ARGS"
    fi

    SPECTRAL_ARGS="maxholdintvl=5000 nfrssi=1 nobeacons=0"


#
# Finally, insert the modules
#
    insmod $MODULE_PATH/adf.ko
    insmod $MODULE_PATH/asf.ko
    insmod $MODULE_PATH/ath_hal.ko
    insmod $MODULE_PATH/ath_rate_atheros.ko
#    insmod $MODULE_PATH/ath_spectral.ko $SPECTRAL_ARGS
    if [ "${AP_NO_A_BAND}" != "1" ]; then
        #load DFS if A band is supported,default is supported and set AP_NO_A_BAND=1 if not supported
        insmod $MODULE_PATH/ath_dfs.ko $DFS_ARGS
    fi
#    insmod $MODULE_PATH/hst_tx99.ko
    insmod $MODULE_PATH/ath_dev.ko
    insmod $MODULE_PATH/umac.ko
#    insmod $MODULE_PATH/wlan_me.ko
    insmod $MODULE_PATH/ath_pktlog.ko

# Remove the temporary file to notify VAP creation  is successful
   rm -f /tmp/.apup

#
# If Country Code has been specified, it needs to be set here using
# the iwpriv command.  This will be common for all VAPs
#

    if [ "$ATH_countrycode" != "" ]; then
        iwpriv radio0 setCountryID $ATH_countrycode
		
	##
	## Check the result of this command.  If the country code
	## is invalid, it will return a non-zero result.  We should
	## die a horrible death in this case
	##

	if [ $? != 0 ]; then
		_ath_unload
		exit 255
	fi

        iwpriv radio1 setCountryID $ATH_countrycode

	##
	## Don't check this one.  If the second module is not installed, we will
	## get an error code.  If the country code is invalid, it was invalid for
	## the previous call, so we won't get here anyway
	##

	exit 0
    fi

else
#
# First, check to see if all of the VAPs have been eliminated
# If not, kill them all.  Don't continue, since this script
# will be called by killVAP all
#

    APS=`iwconfig | grep ath`

    if [ "${APS}" != "" ]; then
        /etc/ath/killVAP all

        #
        # We exit here since the above script will call this script
        # to delete all modules
        #

        exit
    fi

	_ath_unload

fi
